package com.eqdd.yiqidian.utils

import android.app.Activity
import android.graphics.Bitmap
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.ImageView
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.kotlin.BaseRecyclerViewAdapter
import com.eqdd.yiqidian.kotlin.toArrayList
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.layout_image_grid_item.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by jhj on 18-6-28.
 */
object ImageShowUtil {
    val optionsNormal: RequestOptions = RequestOptions()
            .placeholder(R.mipmap.ic_launcher_logo)
            .error(R.mipmap.ic_launcher_logo)


    fun setListImgPreview(activity: Activity, imgUrl: String, imageView: ImageView) {
        val screenWidth = activity.resources.displayMetrics.widthPixels
        Glide.with(activity)
                .asBitmap()
                .load(imgUrl)
                .apply(optionsNormal)
                .into<SimpleTarget<Bitmap>>(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>) {
                        val params: RelativeLayout.LayoutParams
                        if (resource.height > resource.width) {
                            val scale = resource.width.toDouble() / resource.height.toDouble()
                            val height = (screenWidth * 2 / 3).toDouble()
                            val width = height * scale
                            params = RelativeLayout.LayoutParams(width.toInt(), height.toInt())
                        } else if (resource.height < resource.width) {
                            val scale = resource.height.toDouble() / resource.width.toDouble()
                            val width = (screenWidth * 3 / 4).toDouble()
                            val height = width * scale
                            params = RelativeLayout.LayoutParams(width.toInt(), height.toInt())
                        } else {
                            params = RelativeLayout.LayoutParams(screenWidth * 2 / 3, screenWidth * 2 / 3)
                        }
                        imageView.layoutParams = params
                        imageView.setImageBitmap(resource)
                        imageView.onClick {
                            imgPathPreview(activity, 0, listOf(imgUrl))
                        }
                    }
                })

    }


    fun setListImgPreview(activity: Activity, imgUrlList: List<String>, recyclerView: RecyclerView, showNum: Int = 9) {
        if (imgUrlList.size == 1) {
            recyclerView.layoutManager = LinearLayoutManager(activity)
        } else {
            recyclerView.layoutManager = GridLayoutManager(activity, 3)
        }

        ItemClickSupport.addTo(recyclerView).setOnItemClickListener { view, position, v ->
            imgPathPreview(activity, position, imgUrlList)
        }

        val adapter = ImagesAdapter(activity)
        adapter.dataList = imgUrlList.filterIndexed { index, _ -> index < showNum }.toArrayList()
        recyclerView.adapter = adapter

    }


    fun imgPathPreview(activity: Activity, position: Int, imgList: List<String>) {
        val localMediaList = imgList.map {
            LocalMedia(it, 0, PictureMimeType.ofImage(), PictureConfig.IMAGE)
        }
        localMediaImgPreview(activity, position, localMediaList)

    }

    fun localMediaImgPreview(activity: Activity, position: Int, localMediaList: List<LocalMedia>) {
        PictureSelector.create(activity).externalPicturePreview(position, localMediaList)
    }


    class ImagesAdapter(val activity: Activity) : BaseRecyclerViewAdapter<String>() {

        val density: Double = activity.resources.displayMetrics.density.toDouble()

        override fun onCreateView(viewType: Int): Int {
            return R.layout.layout_image_grid_item
        }

        override fun onBindViewItemViewHolder(holder: ItemViewHolder, bean: String, position: Int) {

            if (dataList.size == 1) {
                val screenWidth = activity.resources.displayMetrics.widthPixels - 5 * density
                Glide.with(activity)
                        .asBitmap()
                        .load(bean)
                        .apply(optionsNormal)
                        .into<SimpleTarget<Bitmap>>(object : SimpleTarget<Bitmap>() {
                            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>) {
                                val params: RelativeLayout.LayoutParams
                                if (resource.height > resource.width) {
                                    val scale = resource.width.toDouble() / resource.height.toDouble()
                                    val height = screenWidth * 2 / 3
                                    val width = height * scale
                                    params = RelativeLayout.LayoutParams(width.toInt(), height.toInt())
                                } else if (resource.height < resource.width) {
                                    val scale = resource.height.toDouble() / resource.width.toDouble()
                                    val width = screenWidth * 3 / 4
                                    val height = width * scale
                                    params = RelativeLayout.LayoutParams(width.toInt(), height.toInt())
                                } else {
                                    params = RelativeLayout.LayoutParams((screenWidth * 2 / 3).toInt(), (screenWidth * 2 / 3).toInt())
                                }
                                holder.itemView.iv_picture.let {
                                    it.layoutParams = params
                                    it.setImageBitmap(resource)
                                }
                            }
                        })
            } else {
                val screenWidth = activity.resources.displayMetrics.widthPixels - 5 * density
                val params = RelativeLayout.LayoutParams((screenWidth * 1 / 3).toInt(), (screenWidth * 1 / 3).toInt())
                holder.itemView.iv_picture?.let {
                    it.layoutParams = params
                    it.scaleType = ImageView.ScaleType.CENTER_CROP
                    Glide.with(activity)
                            .asBitmap()
                            .load(bean)
                            .into(it)
                }


            }
        }

    }


}